package ink.tiptop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.tiptop.persist.domain.entity.ZjfPubKeyEntity;
import ink.tiptop.dto.req.base.key.AddZjfPubKeyEntityRequest;
import ink.tiptop.dto.req.base.key.DeleteZjfPubKeyEntityRequest;
import ink.tiptop.dto.req.base.key.QueryZjfPubKeyEntityRequest;
import ink.tiptop.dto.req.base.key.UpdateZjfPubKeyEntityRequest;
import ink.tiptop.enums.EResp;
import ink.tiptop.common.domain.WebworkResponse;
import ink.tiptop.dto.resp.base.ZjfPubKeyInfo;
import ink.tiptop.common.cache.CacheAble;
import ink.tiptop.common.cache.CacheEvict;
import ink.tiptop.common.cache.ECaches;
import ink.tiptop.persist.dao.ZjfBaseServerInfoMapper;
import ink.tiptop.persist.dao.ZjfPubKeyMapper;
import ink.tiptop.common.domain.req.PageRequest;
import ink.tiptop.common.domain.rsp.Response;
import ink.tiptop.service.AbstractBaseService;
import ink.tiptop.service.ZjfPubKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 公钥信息表(ZjfPubKey)表服务实现类
 *
 * @author makejava
 * @since 2020-07-20 16:31:45
 */
@Service("zjfPubKeyService")
@Slf4j
public class ZjfPubKeyServiceImpl extends AbstractBaseService<ZjfPubKeyEntity> implements ZjfPubKeyService {
    private static final String CACHE_KEY = "PUB_KEY";
    private static final String CACHE_CLEAR = CACHE_KEY + "*";
    @Resource
    private ZjfPubKeyMapper zjfPubKeyMapper;
    @Resource
    private ZjfBaseServerInfoMapper baseServerInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ZjfPubKeyEntity queryById(Integer id) {
        return this.zjfPubKeyMapper.selectById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<ZjfPubKeyEntity> queryAll() {
        return this.zjfPubKeyMapper.selectList(null);
    }

    /**
     * 修改数据
     *
     * @param request 实例对象
     * @return 实例对象
     */
    @Override
    @CacheEvict(key = CACHE_CLEAR,type = ECaches.DAY)
    public Response update(UpdateZjfPubKeyEntityRequest request) {
        ZjfPubKeyEntity entity = new ZjfPubKeyEntity();
        entity.setId(request.getId());
        int count = zjfPubKeyMapper.selectCount(new QueryWrapper<>(entity));

        BeanUtils.copyProperties(request, entity);
        if (count < 1) {
            return WebworkResponse.failed(EResp.EKey.ERROR_EXIST_NOT);
        }

        int rows = zjfPubKeyMapper.updateById(entity);
        if (rows > 0) {
            return WebworkResponse.SUCCEED;
        }
        return WebworkResponse.ERROR;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.zjfPubKeyMapper.deleteById(id) > 0;
    }

    @Override
    @CacheAble(desc = "服务器公钥信息查询缓存", keyPrefix = CACHE_KEY,type = ECaches.DAY)
    public Response list(QueryZjfPubKeyEntityRequest zjfPubKeyEntity) {
        return super.list(zjfPubKeyEntity);
    }

    @Override
    @CacheEvict(key = CACHE_CLEAR,type = ECaches.DAY)
    public Response delete(DeleteZjfPubKeyEntityRequest request) {
        // 1.校验公钥是否已经关联了其他表信息(服务器基本信息表)
        // 请求删除的ID列表
        List<Integer> reqIdList = request.getId();
        // 2.查询到关联的id列表
        List<Integer> relatedKeyIdList = baseServerInfoMapper.listRelatedKeyIdList(reqIdList);
        if (relatedKeyIdList != null && !relatedKeyIdList.isEmpty()) {
            // 请求删除的id列表 删除 关联的id列表 , 剩下的是可以都删除的
            List<Integer> delIdList = reqIdList.stream().filter(rq -> !relatedKeyIdList.contains(rq)).collect(Collectors.toList());
            if (delIdList == null || delIdList.isEmpty()) {
                return WebworkResponse.failed(EResp.EKey.ERROR_DELETE_FORBID);
            }
            int rows = zjfPubKeyMapper.deleteBatchIds(delIdList);
            if (rows > 0) {
                return WebworkResponse.failed(EResp.EKey.ERROR_DELETE_PART);
            }
        } else {
            int rows = zjfPubKeyMapper.deleteBatchIds(reqIdList);
            if (rows > 0) {
                return WebworkResponse.SUCCEED;
            }
        }
        return WebworkResponse.ERROR;
    }

    @Override
    @CacheEvict(key = CACHE_CLEAR,type = ECaches.DAY)
    public Response add(AddZjfPubKeyEntityRequest request) {
        ZjfPubKeyEntity entity = new ZjfPubKeyEntity();
        int hashCode = Objects.hashCode(request.getKey());
        entity.setHash(hashCode + "");
        int count = zjfPubKeyMapper.selectCount(new QueryWrapper<>(entity));
        if (count > 0) {
            return WebworkResponse.failed(EResp.EKey.ERROR_EXIST);
        }
        BeanUtils.copyProperties(request, entity);
        int rows = zjfPubKeyMapper.insert(entity);
        if (rows > 0) {
            return WebworkResponse.SUCCEED;
        }
        return WebworkResponse.ERROR;
    }

    @Override
    protected void setOrderBy(PageRequest request) {

    }

    @Override
    protected ZjfPubKeyEntity getQueryParam(PageRequest request) {
        ZjfPubKeyEntity entity = new ZjfPubKeyEntity();
        BeanUtils.copyProperties(request, entity);
        return entity;
    }

    @Override
    public Page<ZjfPubKeyEntity> getQueryEntityList(Page<ZjfPubKeyEntity> page,ZjfPubKeyEntity param) {
        return zjfPubKeyMapper.selectPage(page, new QueryWrapper<>(param));
    }

    @Override
    protected Object getResponseList(List<ZjfPubKeyEntity> voList) {
        List<ZjfPubKeyInfo> infos = new ArrayList<>();
        ZjfPubKeyInfo info = null;
        for (ZjfPubKeyEntity entity : voList) {
            info = new ZjfPubKeyInfo();
            BeanUtils.copyProperties(entity, info);
            infos.add(info);
        }
        return infos;
    }

}